//
//  GWEssenceViewController.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/1.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWEssenceViewController.h"
#import "GWTopicViewController.h"
@interface GWEssenceViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak)UIView *redView;
@property (nonatomic,weak)UIButton *currentButton;

@property (nonatomic,weak)UIScrollView *contentView;
@property (nonatomic,weak)UIView *editorView;
@end

@implementation GWEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets =NO;
    
    
    [self setUpChildView];
    
    [self setUpNav];
    
    
    [self setUpEditor];
    
    
    [self setUpContentView];
}
-(void)setUpChildView
{
    GWTopicViewController *all =[[GWTopicViewController alloc]init];
    all.title =@"全部";
    all.type =GWTopicControllerTypeAll;
    [self addChildViewController:all];
    
    GWTopicViewController *video =[[GWTopicViewController alloc]init];
    video.title =@"视频";
    video.type =GWTopicControllerTypeVideo;
    [self addChildViewController:video];
    
    GWTopicViewController *voice =[[GWTopicViewController alloc]init];
    voice.title =@"声音";
    voice.type =GWTopicControllerTypeVoice;
    [self addChildViewController:voice];
    
    GWTopicViewController *picture =[[GWTopicViewController alloc]init];
    picture.title =@"图片";
    picture.type =GWTopicControllerTypePicture;
    [self addChildViewController:picture];
    
    GWTopicViewController *word =[[GWTopicViewController alloc]init];
    word.title =@"段子";
    word.type =GWTopicControllerTypeWord;
    [self addChildViewController:word];
    
}
-(void)setUpContentView
{
    
    UIScrollView *scrollView =[[UIScrollView alloc]init];
    scrollView.frame =self.view.bounds;
    [self.view insertSubview:scrollView atIndex:0];
    self.contentView =scrollView;
    scrollView.delegate =self;
    scrollView.pagingEnabled =YES;
    scrollView.contentSize =CGSizeMake(self.view.width*self.childViewControllers.count, 0);
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}
-(void)setUpEditor
{
    UIView *editorView =[[UIView alloc]init];
    editorView.backgroundColor =[UIColor colorWithWhite:1.0 alpha:0.8];
    editorView.frame =CGRectMake(0, 64, self.view.width, 35);
    [self.view addSubview:editorView];
    self.editorView =editorView;
    
    UIView *redView =[[UIView alloc]init];
    redView.backgroundColor =[UIColor redColor];
    redView.height = 2;
    redView.y =editorView.height-redView.height;
    self.redView =redView;
    
    
    CGFloat btnW =editorView.width/self.childViewControllers.count;
    CGFloat btnH =editorView.height;
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIButton *btn =[[UIButton alloc]init];
        btn.frame =CGRectMake(btnW*i, 0, btnW, btnH);
        [editorView addSubview:btn];
        [btn setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        btn.titleLabel.font =[UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag =10+i;
        
        if (i==0) {
            
             btn.enabled =NO;
            self.currentButton =btn;
            [btn.titleLabel sizeToFit];
            self.redView.width =btn.titleLabel.width;
            self.redView.centerX =btn.centerX;
        }
    }
    
    
    [editorView addSubview:redView];
}
-(void)btnOnClick:(UIButton *)btn
{
    self.currentButton.enabled =YES;
    btn.enabled =NO;
    self.currentButton =btn;
    [UIView animateWithDuration:0.25 animations:^{
        self.redView.width =btn.titleLabel.width;
        self.redView.centerX =btn.centerX;
    }];
    
    [self.contentView setContentOffset:CGPointMake((btn.tag-10)*self.view.width, 0) animated:YES];
}
-(void)setUpNav
{
    self.navigationItem.titleView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    self.navigationItem.leftBarButtonItem =[UIBarButtonItem itemWithImage:@"MainTagSubIcon" highLightImage:@"MainTagSubIconClick" target:self action:@selector(tagBtnOnClick)];
    
    self.view.backgroundColor =GWGlobalBGColor;
}

-(void)tagBtnOnClick
{
    UIViewController *vc =[[UIViewController alloc]init];
    vc.view.backgroundColor =[UIColor colorWithRed:0.562 green:1.000 blue:0.539 alpha:1.000];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    NSInteger index =scrollView.contentOffset.x/self.view.width;
    UITableViewController *tableView =self.childViewControllers[index];
    tableView.view.x =scrollView.contentOffset.x;
    tableView.view.y =0;
    tableView.view.height =scrollView.height;
    [scrollView addSubview:tableView.view];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index =scrollView.contentOffset.x/self.view.width;
    [self btnOnClick:self.editorView.subviews[index]];
    [self scrollViewDidEndScrollingAnimation:scrollView];
}
@end
